var database = require('../../../data/data.js');
Page({

  data: {
    flag: "none",
    flag3: true,
    shareFlag: false,
  },
  onLoad: function (options) {
    wx.showLoading({
      title: '资源加载中',
      mask: true
    })
    if (options.shareFlag != null) {
      this.setData({
        shareFlag: true
      })
    } else {
      this.setData({
        shareFlag: false
      })
    }
    var that = this;
    this.setData({
      openid: database.userId,
      popenid: options.popenid,
      id: options.gameid
    })
    wx.request({
      url: database.url + '/project/getContestDetail.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        openid: options.popenid,
        id: options.gameid
      },
      success(res) {
        console.log(res)
        wx.request({
          url: database.url1 + '/api/V2/user/get_user',
          method: "POST",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            accessToken: database.accessToken,
            userId: res.data.contest.mopenid,
          },
          success(res) {
            that.setData({
              contact1: res.data.data.mobile
            })
          }
        })
        var baoming = 0;
        var daiding = 0;
        var qingjia = 0;
        if (res.data.players != null) {
          for (var i = 0; i < res.data.players.length; i++) {
            if (res.data.players[i].playerStatus == "报名") {
              baoming++;
            } else if (res.data.players[i].playerStatus == "待定") {
              daiding++;
            } else {
              qingjia++;
            }
          }
          that.setData({
            players: res.data.players,
            playerslength: res.data.players.length,
          })
        }
        that.setData({
          lat: res.data.contest.lat,
          lng: res.data.contest.lng,
          baoming: baoming,
          daiding: daiding,
          qingjia: qingjia
        })
        var contest = res.data.contest;
        var reg = new RegExp("-", "g");
        var newTime = contest.postdate.replace(reg, "/");
        var date = new Date(newTime);
        var date1 = new Date();
        var reg1 = new RegExp("-", "g");
        var newTime1 = contest.date.replace(reg1, "/");
        var date2 = new Date(newTime1);
        var cancle = true;
        if (date2 - date1 > 0) {
          if (parseInt((date2 - date1) / 1000) >= 60) {
            if (parseInt((date2 - date1) / 1000 / 60) >= 60) {
              if (parseInt((date2 - date1) / 1000 / 60 / 60) >= 48) {
                cancle = true;
              } else {
                cancle = false;
              }
            } else {
              cancle = false;
            }
          } else {
            cancle = false;
          }
        } else {
          cancle = false;
        }
        var seconds = 0;
        var minutes = 0;
        var hour = 0;
        var day = 0;
        var month = 0;
        var year = 0;
        var timeDifference = "";
        if ((date1 - date) / 1000 >= 60) {
          if ((date1 - date) / 1000 / 60 >= 60) {
            if ((date1 - date) / 1000 / 60 / 60 >= 24) {
              if ((date1 - date) / 1000 / 60 / 60 / 24 >= 30) {
                if ((date1 - date) / 1000 / 60 / 60 / 24 / 30 >= 12) {
                  year = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30 / 12);
                  timeDifference = year + "年前";
                } else {
                  month = parseInt((date1 - date) / 1000 / 60 / 60 / 24 / 30);
                  timeDifference = month + "个月前";
                }
              } else {
                day = parseInt((date1 - date) / 1000 / 60 / 60 / 24);
                timeDifference = day + "天前";
              }
            } else {
              hour = parseInt((date1 - date) / 1000 / 60 / 60);
              timeDifference = hour + "小时前";
            }
          } else {
            minutes = parseInt((date1 - date) / 1000 / 60);
            timeDifference = minutes + "分钟前";
          }
        } else {
          seconds = parseInt((date1 - date) / 1000);
          timeDifference = seconds + "秒前";
        };
        that.setData({
          timeDifference: timeDifference,
          data: contest,
          cancle: cancle
        });
        wx.hideLoading();
      }
    })
  },
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from != 'menu') {
      var identity = '';
      if (database.userId == this.data.data.popenid) {
        identity = 'p';
      } else if (database.userId == this.data.data.mopenid) {
        identity = 'm';
      }
      this.setData({
        flag: ""
      })
      return {
        title: "比赛即将开始，队友们快来报名",
        path: "/pages/join_game/join_game?identity=" + identity + "&popenid=" + database.userId + "&gameid=" + this.data.data.gameid,
        complete: function (res) {
          wx.showToast({
            title: '邀请成功',
            icon: 'success',
            duration: 1500
          })
          that.setData({
            flag: "none"
          })
        },
      }
    } else {
      return {
        title: '比赛详情',
        path: "/pages/fight_detail/publisher/publisher?popenid=" + that.data.popenid + "&gameid=" + that.data.id + "&shareFlag=true",
        success: function (res) {
          wx.showToast({
            title: '转发成功',
            icon: 'success',
            duration: 1500
          })
        }
      }
    }
  },
  toTeamDetail: function (e) {
    var teamid = e.currentTarget.dataset.teamid;
    var userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/my/myteam/mainpage1/mainpage1?id=' + teamid + '&userid=' + userid,
    })
  },
  showLocation: function () {
    var that = this;
    wx.openLocation({
      latitude: parseFloat(that.data.lat),
      longitude: parseFloat(that.data.lng),
    })
  },
  cancel1: function (e) {
    this.setData({
      flag3: true
    })
  },
  textareaChange1: function (e) {
    if (e.detail.value != "") {
      this.setData({
        flag4: true,
        reason: e.detail.value
      })
    } else {
      this.setData({
        flag4: false,
        reason: ""
      })
    }
  },
  cancelYuezhan: function (e) {
    var that = this;
    if (database.userId == that.data.mopenid) {
      var mopenid = that.data.popenid
    } else {
      var mopenid = that.data.mopenid
    }
    wx.request({
      url: database.url + '/project/uploadCancelYuezhan.action',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      data: {
        id: that.data.gameid,
        popenid: database.userId,
        mopenid: mopenid,
        reasons: that.data.reason
      },
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        that.setData({
          flag3: true
        })
        if (res.data == 0) {
          wx.showToast({
            title: '发送成功!',
          })
        } if (res.data == -2) {
          wx.showToast({
            title: '您已通知过了!',
          })
        }
      }
    });
  },
  cancelGame: function (e) {
    if (this.data.cancle) {
      this.setData({
        flag3: false,
        gameid: e.currentTarget.dataset.gameid,
        mopenid: e.currentTarget.dataset.mopenid,
        popenid: e.currentTarget.dataset.popenid
      })
    } else {
      wx.showModal({
        content: '赛事马上就要开始，您不能取消赛事！',
      })
    }
  },
  call: function (e) {
    wx.showActionSheet({
      itemList: ["拨打电话", e.currentTarget.dataset.tel],
      success: function (res) {
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.tel,
        })
      },
    })
  },
})